/*
var mysql = require('mysql');
var mssql = require('mssql');
var path = require("path");
var fs = require("fs");
var shelljs = require('shelljs');
var mqtt = require('mqtt');
var express = require('express');
var bodyParser = require('body-parser');
var fs = require('fs');
var https = require('https');
var http = require('http');
var format = require('string-format');
var kurento = require("kurento-client");
*/
var path = require("path");


var db = require("./libs/db");
var model = require("./libs/kmsModelSvr");
var websvr = require("./libs/webSvr");

var svr = {
    db: new db.TDBMySql(),
    mqClient: new model.TMqClient(),
    model: new model.TKmsModelSvr(),
    websvr: new websvr.TWebSvr(),

    start() {
        var me = svr;
        return new Promise((success, faild) => {
            try {
                me.initConfig();
                me.websvr.start().then(_ => {
                    //    return me.model.start();
                    //}).then(_ => {
                    //    return me.model.clearObjs();
                    //}).then(_ => {
                    success();
                });
            } catch (error) {
                console.log(error);
                success();
            }
        })
    },
    initConfig() {
        var me = svr;
        try {
            // ----------------- db --------------------- 
            me.db.setConfig("re", {
                host: "dq.hhdata.cn",
                port: 31000,
                uid: "root",
                pwd: "hhuc1115",
                db: "remoteExaminationV10",
                sqlPath: path.join(__dirname, 'datas/sqls')
            });
            me.db.regist2WebSvr(me.websvr);
            // ----------------- mqtt / model---------------------
            me.mqClient.config = {
                mqClientId: ['mqid', ((new Date()).getTime()).toString(), parseInt(1000 * Math.random()).toString()].join(''),
                mqUrl: "tcp://hhuciot.mqtt.iot.bj.baidubce.com:1883",
                mqUid: "hhuciot/mcu",
                mqPwd: "9vG6KT6V64S2uS3P"
            };
            me.model.config = {
                clientId: ['c', (new Date()).getTime(), parseInt(100 * Math.random())].join(""),
                version: "kmsModel.v21",
                recorderPath: "/data/rec",
                kmsUrl: "wss://wj.hhdata.cn:10104/kurento",
                clientKmsUrl: "wss://wj.hhdata.cn:10104/kurento",
                peerOptions: {
                    configuration: {
                        "iceServers": [{
                                url: "stun:39.171.53.53:3478"
                            },
                            {
                                urls: ["turn:39.171.53.53:3478"],
                                username: "kurento",
                                credential: "kurento"
                            }
                        ]
                    },
                    mediaConstraints: {
                        audio: true,
                        video: {
                            width: { min: 1280, ideal: 1920, max: 1920 },
                            height: { min: 720, ideal: 1080, max: 1080 }
                        },
                    }
                },
                checkObjTimeout: 120000
            };
            me.model.mqClient = me.mqClient;
            me.model.regist2WebSvr(me.websvr);
            //----------------------------  web server --------------------
            me.websvr.config = {
                port: 31023,
                domain: "meeting.xmgkfw.com",
                https: {
                    enable: true,
                    keyFn: path.join(__dirname, 'datas/keys/7219068_meeting.xmgkfw.com.key'),
                    certFn: path.join(__dirname, 'datas/keys/7219068_meeting.xmgkfw.com.pem')
                },
                statics: {
                    "/": path.join(__dirname, 'static')
                }
            };
        } catch (error) {
            console.log(error);
        }
    }
};

svr.start();